package cn.hhz._05mvcboot02.controller;

import cn.hhz._05mvcboot02.mapper.UserMapper;
import cn.hhz._05mvcboot02.pojo.dto.UserInsertDTO;
import cn.hhz._05mvcboot02.pojo.entity.User;
import cn.hhz._05mvcboot02.pojo.entity.Userinfov0;
import cn.hhz._05mvcboot02.pojo.vo.UserListVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "用户模块")
//@Controller
@RestController
@RequestMapping("v1/users/")
public class UserController {

    @Autowired
    UserMapper userMapper;
    @PostMapping("insert")
    public String insert(UserInsertDTO userInsertDTO){
        User user = new User();
        BeanUtils.copyProperties(userInsertDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "添加成功";
    }

    @GetMapping("userList")
    public List<UserListVO> userListVOList(){
    return userMapper.selectUserList();
    }

    @GetMapping("delete")
    public String userdel(int id){
        userMapper.delUser(id);
        return "删除成功";
    }

    @PostMapping("update")
    public String userup(User user){
        userMapper.upuser(user);
        return "修改成功";
    }

    @GetMapping("{id}")
    public Userinfov0 getUserinfo(@PathVariable int id){
    return userMapper.selectUserinfoByid(id);
    }

}
